<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户唯一标识"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCardNumber">
        <el-input
          v-model="queryParams.idCardNumber"
          placeholder="请输入身份证号码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入年龄"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="申请状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择申请状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.APPLY_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"> <Icon icon="ep:search" class="mr-5px" /> 搜索 </el-button>
        <el-button @click="resetQuery"> <Icon icon="ep:refresh" class="mr-5px" /> 重置 </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:credit-application:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:credit-application:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="用户唯一标识" align="center" prop="userId" />
      <el-table-column label="身份证号码" align="center" prop="idCardNumber" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="手机号码" align="center" prop="phoneNumber" />
      <el-table-column label="身份证正面照片" align="center" prop="idCardFrontImage">
        <template #default="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.idCardFrontImage"
            :zoom-rate="1.2"
            :preview-src-list="[scope.row.idCardFrontImage]"
            :initial-index="0"
            fit="cover"
            hide-on-click-modal
            preview-teleported
          />
        </template>
      </el-table-column>
      <el-table-column label="身份证反面照片" align="center" prop="idCardBackImage">
        <template #default="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.idCardBackImage"
            :zoom-rate="1.2"
            :preview-src-list="[scope.row.idCardBackImage]"
            :initial-index="0"
            fit="cover"
            hide-on-click-modal
            preview-teleported
          />
        </template>
      </el-table-column>
      <el-table-column label="面部照片" align="center" prop="faceImage">
        <template #default="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.faceImage"
            :zoom-rate="1.2"
            :preview-src-list="[scope.row.faceImage]"
            :initial-index="0"
            fit="cover"
            hide-on-click-modal
            preview-teleported
          />
        </template>
      </el-table-column>
      <el-table-column label="工作单位名称" align="center" prop="companyName" />
      <el-table-column label="工作地址" align="center" prop="companyAddress" />
      <el-table-column label="职位" align="center" prop="position" />
      <el-table-column label="年收入" align="center" prop="annualIncome" />
      <el-table-column label="婚姻状况" align="center" prop="maritalStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MARRY_STATUS" :value="scope.row.maritalStatus" />
        </template>
      </el-table-column>
      <el-table-column label="学历" align="center" prop="education">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.EDUCATIONAL_BACKGROUND" :value="scope.row.education" />
        </template>
      </el-table-column>
      <el-table-column label="紧急联系人1姓名" align="center" prop="contact1Name" />
      <el-table-column label="紧急联系人1电话" align="center" prop="contact1Phone" />
      <el-table-column label="紧急联系人1关系" align="center" prop="contact1Relationship">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.EMERGENCY_CONTACT_RELATIONSHIP"
            :value="scope.row.contact1Relationship"
          />
        </template>
      </el-table-column>
      <el-table-column label="紧急联系人2姓名" align="center" prop="contact2Name" />
      <el-table-column label="紧急联系人2电话" align="center" prop="contact2Phone" />
      <el-table-column label="紧急联系人2关系" align="center" prop="contact2Relationship">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.EMERGENCY_CONTACT_RELATIONSHIP"
            :value="scope.row.contact2Relationship"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="申请时间"
        align="center"
        prop="applicationTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="申请状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.APPLY_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="是否缴纳公积金" align="center" prop="isHousingFund">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isHousingFund" />
        </template>
      </el-table-column>
      <el-table-column label="是否缴纳社保" align="center" prop="isSocialInsurancePaid" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isSocialInsurancePaid" />
        </template>
      </el-table-column>
      <el-table-column label="工资发放银行" align="center" prop="salaryBank" />
      <el-table-column label="营业执照照片" align="center" prop="businessLicense">
        <template #default="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.businessLicense"
            :zoom-rate="1.2"
            :preview-src-list="[scope.row.businessLicense]"
            :initial-index="0"
            fit="cover"
            hide-on-click-modal
            preview-teleported
          />
        </template>
      </el-table-column>
      <el-table-column label="是否有营业执照" align="center" prop="hasBusinessLicense">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.hasBusinessLicense" />
        </template>
      </el-table-column>
      <el-table-column label="是否有银行流水" align="center" prop="hasBankFlow">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.hasBankFlow" />
        </template>
      </el-table-column>
      <el-table-column label="是否真实经营" align="center" prop="isRealOperation">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isRealOperation" />
        </template>
      </el-table-column>
      <el-table-column label="是否有负债" align="center" prop="hasDebt">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.hasDebt" />
        </template>
      </el-table-column>
      <el-table-column label="负债金额" align="center" prop="debtAmount" />
      <el-table-column label="负债类型" align="center" prop="debtType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.DEBT_TYPE" :value="scope.row.debtType" />
        </template>
      </el-table-column>
      <el-table-column label="是否有逾期" align="center" prop="hasOverdue">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.hasOverdue" />
        </template>
      </el-table-column>
      <el-table-column label="逾期天数" align="center" prop="overdueDays" />
      <el-table-column label="每月还款额" align="center" prop="monthlyRepayment" />
      <el-table-column label="是否有官司" align="center" prop="hasLawsuit">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.hasLawsuit" />
        </template>
      </el-table-column>
      <el-table-column label="官司描述" align="center" prop="lawsuitDescription" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:credit-application:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:credit-application:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CreditApplicationForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { CreditApplicationApi, CreditApplicationVO } from '@/api/system/creditapplication'
import CreditApplicationForm from './CreditApplicationForm.vue'

/** 授信信息 列表 */
defineOptions({ name: 'CreditApplication' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<CreditApplicationVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: undefined,
  idCardNumber: undefined,
  name: undefined,
  age: undefined,
  phoneNumber: undefined,
  idCardFrontImage: undefined,
  idCardBackImage: undefined,
  faceImage: undefined,
  companyName: undefined,
  companyAddress: undefined,
  position: undefined,
  annualIncome: undefined,
  maritalStatus: undefined,
  education: undefined,
  contact1Name: undefined,
  contact1Phone: undefined,
  contact1Relationship: undefined,
  contact2Name: undefined,
  contact2Phone: undefined,
  contact2Relationship: undefined,
  applicationTime: [],
  status: undefined,
  isHousingFund: undefined,
  isSocialInsurancePaid: undefined,
  salaryBank: undefined,
  businessLicense: undefined,
  hasBusinessLicense: undefined,
  hasBankFlow: undefined,
  isRealOperation: undefined,
  hasDebt: undefined,
  debtAmount: undefined,
  debtType: undefined,
  hasOverdue: undefined,
  overdueDays: undefined,
  monthlyRepayment: undefined,
  hasLawsuit: undefined,
  lawsuitDescription: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CreditApplicationApi.getCreditApplicationPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CreditApplicationApi.deleteCreditApplication(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    // const data = await CreditApplicationApi.exportCreditApplication(queryParams)
    const data = await CreditApplicationApi.exportViewCreditApplication(queryParams)
    download.excel(data, '授信信息.xlsx')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
